<?php
/*
 * @Descripttion: 
 * @Author: dedeye
 * @Date: 2020-02-25 21:51:01
 * @LastEditors: dedeye
 * @LastEditTime: 2020-02-28 22:54:07
 */

namespace app\common\util;

class Tool
{
    public static function opensslEncode($data = '', $iv = '')
    {
        if($data == '') return false;
        $aesConfig = config('admin.aes');
        if($iv == '') {
            $iv = $aesConfig['iv'];
        }
        $encode = openssl_encrypt($data, $aesConfig['method'], $aesConfig['key'], $aesConfig['options'], $iv);
        return base64_encode($encode);
    }

    public static function opensslDecode($data = '', $iv = '')
    {
        if($data == '') return false;
        $aesConfig = config('admin.aes');
        if($iv == '') {
            $iv = $aesConfig['iv'];
        }
        $data = base64_decode($data);
        return openssl_decrypt($data, $aesConfig['method'], $aesConfig['key'], $aesConfig['options'], $aesConfig['iv']);
    }

    // 无限级树形菜单
    public static function getMenu($data, $pid = 0, $deep = 0)
    {
        $tree = [];
        foreach ($data as $k => $v) {
            if($v['fid'] == $pid){
                $son = self::getMenu($data, $v['id'], $deep + 1);
                if ($deep == 0) {
                    $tree[$v['hash']] = $v;
                    if(count($son) > 0)
                        $tree[$v['hash']]['child'] = $son;
                } else {
                    if(count($son) > 0)
                        $v['child'] = $son;
                    $tree[] = $v;
                }
            }
        }
        return $tree;
    }
}